/**
 * @Company:中享思途   
 * @Title:ProductManagerController.java 
 * @Author:IL Clown   
 * @Date:2020年8月20日 下午8:38:02     
 */
package com.situ.shopsystem.controller;

import java.io.Serializable;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.situ.shopsystem.pojo.Catalog;
import com.situ.shopsystem.pojo.LayuiResult;
import com.situ.shopsystem.pojo.LayuiUpdateResult;
import com.situ.shopsystem.pojo.Product;
import com.situ.shopsystem.service.CatalogService;
import com.situ.shopsystem.service.ProductService;

/** 
 * @ClassName:ProductManagerController 
 * @Description:(这里用一句话描述这个类的作用)  
 * 
 * manager/product/goIndex
 * manager/product/doAddOrUpdate
 * manager/product/findAllProduct
 * manager/product/delete
 * manager/product/changeState
 * manager/product/findCatalogListByParentId
 * manager/product/uploadFile
 * 
 * 
 * 
 * 
 * 
 */
@Controller
@RequestMapping("/manager/product/")
public class ProductManagerController implements Serializable {
	private static final long serialVersionUID = 1L;
	@Autowired
	private ProductService productService;
	@Autowired
	private CatalogService catalogService;

	@RequestMapping("goIndex")
	public String goIndex() {

		return "manager/product/product_index";
	}

	@RequestMapping("goAdd")
	public String goAdd(Model model) {
		model.addAttribute("catalogList", catalogService.findAllByParent(-1L));
		return "manager/product/product_add_edit";

	}

	@ResponseBody
	@RequestMapping("uploadFile")
	public LayuiUpdateResult uploadFile(MultipartFile multipartFile, HttpServletRequest request, LayuiUpdateResult layuiUpdateResult) {
		String realPath = request.getServletContext().getRealPath("/");
		String path = productService.uploadFile(multipartFile, realPath);
		layuiUpdateResult.setCode(0);
		layuiUpdateResult.setData(path);
		return layuiUpdateResult;
	}

	@ResponseBody
	@RequestMapping("findAllProduct")
	public LayuiResult findAllProduct(Integer page, Integer limit, Product product) {
		return productService.findAll(page, limit, product);
	}

	@ResponseBody
	@RequestMapping("findCatalogListByParentId")
	public List<Catalog> findCatalogListByParentId(Long parentId) {
		return catalogService.findAllByParent(parentId);
	}

	@ResponseBody
	@RequestMapping("doAddOrUpdate")
	public int doAddOrUpdate(Product product, HttpSession session) {
		if (product.getRowId() == null) {
			return productService.save(product, session);
		} else {
			return productService.update(product, session);
		}

	}

	@ResponseBody
	@RequestMapping("delete")
	public int delete(Long rowId) {
		return productService.delete(rowId);
	}

	@ResponseBody
	@RequestMapping("changeState")
	public int changeState(Long rowId) {
		productService.changeState(rowId);
		return 1;
	}

}
